package com.isoft.c2team3service2.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class PwdSaltUtil {
    private static final String HEX_NUMS_STR="0123456789ABCDEF";
    private static final Integer SALT_LENGTH = 16;
//    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
//        String salt = getSalt() ;
//        System.out.println(salt + "," + salt.length());
//        String pwd = getEncryptedPwd("123" , salt) ;
//        System.out.println(pwd + "," + pwd.length());
////        7DB75A3817519BDE05F6E08755471070,32
////        D45BAD79FF7EEABF48481E25DE6C896B,32
////        boolean r = validPassword("123" , "2B6B26AC89C9802D09C6D5FD1100373E" , "907A4EC8240327EA5104E9E717145158") ;
////        System.out.println(r);
//    }

    private static byte[] getSaltByte()  {
        SecureRandom sr = new SecureRandom();
        byte[] salt = new byte[SALT_LENGTH];
        sr.nextBytes(salt);
        return salt;
    }

    public static String getSalt() {
        return byteToHexString(getSaltByte()) ;
    }


    /**
     * 验证口令是否合法
     */
    public static boolean validPassword(String password, String salt, String passwordInDb) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String temp = getEncryptedPwd(password , salt) ;
        if(temp.equals(passwordInDb)) {
            return true ;
        } else {
            return false ;
        }
    }
    /**
     * 获得加密后的16进制形式口令
     */
    public static String getEncryptedPwd(String password , String saltStr) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] salt = hexStringToByte(saltStr) ;
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes("UTF-8"));
        byte[] digest = md.digest();
        byte[] pwdSalt = new byte[salt.length + digest.length] ;
        System.arraycopy(salt, 0, pwdSalt, 0, SALT_LENGTH);
        System.arraycopy(digest, 0, pwdSalt, SALT_LENGTH, digest.length);
        md.update(pwdSalt);
        byte[] result = md.digest() ;
        String pwdSaltStr = byteToHexString(result) ;
        return  pwdSaltStr;
    }


    //将16进制字符串转换成字节数组
    public static byte[] hexStringToByte(String str) {
        int len = (str.length() / 2);
        byte[] result = new byte[len];
        char[] hexChars = str.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4
                    | HEX_NUMS_STR.indexOf(hexChars[pos + 1]));
        }
        return result ;
    }

    // 将指定byte数组转换成16进制字符串
    public static String byteToHexString(byte[] b) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            hexString.append(hex.toUpperCase());
        }
        return hexString.toString();
    }

}
